package com.hw.model;

import com.hw.common.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Json响应实体
 *
 * @author whw
 * @date 2021/6/15
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> {
    /**
     * 响应码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public static <T> ResponseResult<T> success() {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResponseCode.SUCCESS.getCode());
        result.setMessage(ResponseCode.SUCCESS.getMsg());
        return result;
    }

    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResponseCode.SUCCESS.getCode());
        result.setMessage(ResponseCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static ResponseResult<?> error() {
        return error(ResponseCode.FAIL.getMsg());
    }

    public static ResponseResult<?> error(String msg) {
        ResponseResult<?> result = new ResponseResult<>();
        result.setMessage(msg);
        result.setCode(ResponseCode.FAIL.getCode());
        return result;
    }

    public static ResponseResult<?> error(Integer code, String msg) {
        ResponseResult<?> result = new ResponseResult<>();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    public static ResponseResult<?> error(ResponseCode responseCode) {
        ResponseResult<?> result = new ResponseResult<>();
        result.setCode(responseCode.getCode());
        result.setMessage(responseCode.getMsg());
        return result;
    }
}
